/*
 * Topic: 系统依赖（Dependence of System)
 * Author: Yuanshun L
 * Written: 2-Nov-2021
 */

#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<string>
#include<sstream>
using namespace std;

const int maxn= 100+10;


map<string,int> id; // 将模块映射为整型的id
map<int,string> strid; // 将id映射为模块名
int status[maxn]; // 模块安装的状态，0代表没有安装，1代表显式安装，2代表隐式安装
vector<int> depend[maxn]; // 依赖i的模块
vector<int> module[maxn]; // 模块j的依赖

int index=0;

int getId(string &s){
    int i;
    if(id.count(s))
        i = id[s];
    else{
        i = index++; id[s] = i;
        strid[i] = s;
    }
    return i;
}

void install(int ind,int flag){
    if(status[ind]){
        cout<<"  "<<strid[ind]<<" is already installed."<<endl;
        return;
    }
    for(int i=0;i<module[ind].size();i++){
        if(status[module[ind][i]] == 0){
            install(module[ind][i],2);
        }
    }
    status[ind] = flag;
    cout<<"   "<<"Installing "<<strid[ind]<<endl;
}

//  determine whether any module defined by a module of ind doesn't exist.
bool allModulesDeleted(int ind){
    int j;
    for(int i=0;i<depend[ind].size();i++){
        j = depend[ind][i];
        if(status[j]) return false;
    }
    return true;
}

// remove a module
void remove(int ind){
    if(status[ind] == 0){
        cout << "   "<< strid[ind] << " is not installed." << endl;
    }
    else if(allModulesDeleted(ind)){
        int j;
        status[ind] = 0;
        for(int i=0;i<module[ind].size();i++){
            j = module[ind][i];
            if(status[j] == 2 && allModulesDeleted(j)){
                remove(j);
            }
        }
        cout << "   " << "Removing " << strid[ind] << endl;
    }
    else{
        cout << "   " << strid[ind] << " is stilled needed." <<  endl;
    }
}


// display all existing modules.
void list(){
    for(int i=0;i<index;i++){
        if(status[i])
            cout<< "   " << strid[i] << endl;
    }
}


int main(){

    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);

    string s,cmd,modu;
    memset(status,0,sizeof(status));
    while(getline(cin,s) && s[0] !='E'){ // 拿到一行命令

        cout << s << endl;

        stringstream ss(s);
        int id1,id2;
        ss >> cmd;

        switch(cmd[0]){
            case 'D': // 增加依赖
                ss >> modu;
                id1 = getId(modu);
                while(ss>>modu){
                    id2 = getId(modu);
                    depend[id2].push_back(id1);
                    module[id1].push_back(id2);
                }
                break;
            case 'I':  // 安装模块
                ss >> modu;
                install(getId(modu),1);
                break;
            case 'R': // 删除模块
                ss >> modu;
                remove(getId(modu));
                break;
            case 'L': // 模块列表
                list();
                break;
        }

    }
    cout << "END" <<endl;

    return 0;
}